################################################################################
###To project the models###
timeline='Post_decline'
work.dir= paste('/home/jc148322/frog_declines/SDM/regular/',timeline,'/models/',sep='') #your /models/ directory
mxe.dir = '/home/jc165798/Climate/CIAS/AWT/250m/bioclim_mxe/'
maxent.jar = "/home/jc165798/working/NARP_birds/maxent.jar"
proj.list = list.files(mxe.dir) #list the projections
proj.list = proj.list[grep('current',proj.list)] #subset it to simply current and 2080 data

species = list.files(work.dir) #get a list of species

#cycle through each of the species
for (spp in species) { #spp=species[1]
	spp.dir = paste(work.dir,spp,'/',sep=''); setwd(spp.dir) #set the working directory to the species directory

	zz = file(paste('02.',spp,'.project.models.sh',sep=''),'w') ##create the sh file
		cat('#!/bin/bash\n',file=zz)
		cat('cd ',spp.dir,'\n',sep='',file=zz)
		cat('source /etc/profile.d/modules.sh\n',file=zz)
		cat('module load java\n',file=zz)
		dir.create('output/ascii/',recursive=TRUE) #create the output directory for all maps
		#cycle through the projections
		for (tproj in proj.list) cat('java -mx1024m -cp ',maxent.jar,' density.Project ',spp.dir,'output/',spp,'.lambdas ',mxe.dir,tproj,' ',spp.dir,'output/ascii/',tproj,'.asc fadebyclamping nowriteclampgrid\n',sep="",file=zz)
		cat('gzip ',spp.dir,'output/ascii/*asc\n',sep='',file=zz)#Try not to zip
	close(zz)

	#submit the script
	system(paste('qsub -m n 02.',spp,'.project.models.sh',sep=''))
	}

 